<template>
  <div class="directive">
    <!-- v-copy -->
    <p>v-copy 实现</p>
    <section>
      <input type="text" v-model="title" />
      <p class="copy" v-copy="title">{{ title }}</p>
    </section>
    <!-- v-emoji -->
    <p>v-emoji 实现<i>（不可输入特殊字符）</i></p>
    <section>
      <input v-emoji type="text" v-model="inpText" />
      <span>{{ inpText }}</span>
    </section>
    <!-- v-debounce -->
    <!-- 
      传递的形式有： 
        v-debounce="fn" 直接传函数 
        v-debounce={ func:fn,type:'click',params:[10,20]} 传一个对象
    -->
    <p>v-debounce / v-throttle实现<i>（打开 console 面板查看）</i></p>
    <section>
      <button @click="fn">点我（没做防抖）</button>
      <button v-debounce:300.immediate="fn">点我（做了防抖）</button>
      <button v-debounce:300.immediate="{ func: fn2, params: [10, 20] }">点我（做了防抖，还可以传递参数）</button>
      <button v-throttle:300="fn">点我（做了节流）</button>
    </section>
    <!-- v-permission -->
    <p>v-permissions 实现</p>
    <section>
      <button class="danger" v-if="isLogin" @click="isLogin = !isLogin">退出</button>
      <button v-if="!isLogin" v-debounce:300.immediate="submit">登录</button>
      <div v-else>
        <button v-permission="'select'">查询</button>
        <button v-permission="'insert'">新增</button>
        <button v-permission="'update'">修改</button>
        <button v-permission="'delete'">删除</button>
      </div>
    </section>
    <!-- v-lazyload -->
    <p>v-lazyload 实现<i>（第一张图是未做图片懒加载处理的）</i></p>
    <section>
      <!-- 第一张是未做图片懒加载处理的，注意看滚动条的变化 -->
      <img src="@/assets/images/img1.png" alt="" />
      <template v-for="img in imgs">
        <img src="" v-lazyload="img.src" :key="img.id" />
      </template>
    </section>
  </div>
</template>

<script>
import { query_permission } from '@/api/index.js'
export default {
  data() {
    return {
      inpText: 'hello world',
      title: '点我即可复制',
      isLogin: false,
      imgs: [
        {
          id: 1,
          src: 'https://t7.baidu.com/it/u=3569419905,626536365&fm=193&f=GIF'
        },
        {
          id: 2,
          src: 'https://t7.baidu.com/it/u=3779234486,1094031034&fm=193&f=GIF  '
        },
        {
          id: 3,
          src: 'https://t7.baidu.com/it/u=3911840071,2534614245&fm=193&f=GIF'
        }
      ]
    }
  },
  methods: {
    fn() {
      console.log('fn...')
    },
    fn2(a, b) {
      console.log('fn...', a, b)
    },
    async submit() {
      let result = await query_permission()
      alert('登录成功')
      this.isLogin = true
      localStorage.setItem('vue2-directive-permission', result)
    }
  },
  mounted() {
    // setTimeout(() => {
    //   this.title = 'hello world'
    // }, 1000)
  }
}
</script>

<style lang='scss' scoped>
.directive {
  overflow-y: none;
  overflow-y: auto;
  section {
    overflow: auto;
    padding: 20px;
    margin: 10px auto;
    border: solid 01px #ccc;
    width: 90%;
    display: flex;
    flex-flow: row wrap;
    // justify-content: center;
    align-items: center;
    input {
      margin-right: 10px;
      border: solid 1px #ddd;
      padding: 6px;
      outline: none;
    }
    p.copy {
      border: solid 2px #eee;
      padding: 6px;
      cursor: pointer;
      background-color: #16a085;
      color: #fff;
    }
    button {
      border: solid 1px #ddd;
      background: #d35400;
      color: #fff;
      padding: 6px 8px;
      margin: 4px;
      border-radius: 10px;
      cursor: pointer;
      &.danger {
        background: #f1c40f;
      }
      &:hover {
        background: #c0392b;
      }
    }
    img {
      transition: all 0.5s;
      width: 80%;
      margin: 15px 0;
    }
  }
}
</style>
